////
//  UIGroupViewController.m
//  Nav
//
//  Created by hisun on 11-11-11.
//  Copyright 2011 HISUNSRAY.wangming. All rights reserved.
//

#import "UIGroupViewController.h"


@implementation UIGroupViewController

@synthesize resultList,retKeys,TimeObjInfo;

-(IBAction)btn_cancel
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (id)initWithResultList:(NSDictionary*)retList :(NSMutableDictionary *)TObjInfo{
    if (self = [super init]) {
      
        if (retList) {
            self.resultList = retList;
            self.retKeys =  [resultList allKeys];
        }
        self.TimeObjInfo = TObjInfo;
        [self setHidesBottomBarWhenPushed:YES];
    }
    return self;
}


//// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"登记完成" style:(UIBarButtonItemStylePlain) target:self action:@selector(btn_ok)];
	self.navigationItem.rightBarButtonItem = editButton;
	[editButton release];
    
    UIBarButtonItem *CancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(btn_cancel)];
	self.navigationItem.leftBarButtonItem = CancelButton;
	[CancelButton release];
    [super viewDidLoad];
    self.title = @"完成情况";
    retCode = -100;
    lastSelectRow = -1;
    lastSelectSection = -1;
}
//
-(void)btn_ok
{
    if ( retCode == -100) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message: @"请选择完成情况的选项！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil ];
        [alert show];
        [alert release];
        return;
    }
    [self.TimeObjInfo setValue:[NSString stringWithFormat:@"%d",retCode] forKey:@"result"];
    NSDictionary*  ObjFinish = [[MyClass getInstance] finishPlan:self.TimeObjInfo];
    if ([[ObjFinish objectForKey:@"code"] intValue]==0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"登记完成操作已成功！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil ];
        [alert show];
        [alert release];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message: [NSString stringWithFormat:@"%@",[ObjFinish objectForKey:@"error"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil ];
        [alert show];
        [alert release];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}
//

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    self.resultList = nil;
    self.retKeys = nil;
    self.TimeObjInfo= nil;
    [super dealloc];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (curCell) {
        if (!(lastSelectSection == indexPath.section && lastSelectRow== indexPath.row))
            curCell.accessoryType =UITableViewCellAccessoryNone;
    }
    lastSelectRow = indexPath.row;
    lastSelectSection = indexPath.section;
    curCell = [tableView cellForRowAtIndexPath:indexPath];
    if (curCell.accessoryType ==UITableViewCellAccessoryCheckmark) {
        curCell.accessoryType =UITableViewCellAccessoryNone;
        if (retCode == [[resultList valueForKey:curCell.textLabel.text] intValue]) {
            retCode = -100;
        }
    }
    else{
        curCell.accessoryType =UITableViewCellAccessoryCheckmark;
        retCode =[[resultList valueForKey:curCell.textLabel.text] intValue];
    }
    [curCell setSelected:NO];
}
#pragma mark -
-(NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section
{
	return [retKeys count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView
       cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString  *CellIdentifier = @"CellIdentifier";
	UITableViewCell		*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (nil == cell) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier: CellIdentifier ] autorelease];
        cell.showsReorderControl = YES;
	}
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
	NSUInteger row = [indexPath row];
    NSString * key= [retKeys objectAtIndex:row];
    [cell textLabel].text = [resultList objectForKey:key];	
    cell.accessoryType = UITableViewCellAccessoryNone;
    if ([key intValue]==retCode) {
       cell.accessoryType =UITableViewCellAccessoryCheckmark; 
    }
	return cell;
}



@end
